當前位置: 首頁> 函數類別大全> property_exists

property_exists

檢查對像或類是否具有該屬性
名稱:property_exists
分類:類和對象
所屬語言:php
一句話介紹:檢查對像或類是否具有指定的屬性

函數名稱:property_exists()

函數描述:property_exists() 函數檢查對像或類是否具有指定的屬性。

參數:

  • $class:必需。要檢查的類名或對象。
  • $property:必需。要檢查的屬性名。

返回值:

  • 若屬性存在且可訪問,則返回true。
  • 若屬性不存在或不可訪問,則返回false。

適用版本:PHP 4, PHP 5, PHP 7

用法示例:

  1. 檢查類是否具有指定屬性:
 class MyClass { public $name = "John"; private $age = 25; } $object = new MyClass(); if (property_exists($object, 'name')) { echo "The 'name' property exists."; } else { echo "The 'name' property does not exist."; } // 输出:The 'name' property exists.
  1. 檢查對像是否具有指定屬性:
 class MyClass { public $name = "John"; private $age = 25; } $object = new MyClass(); if (property_exists($object, 'age')) { echo "The 'age' property exists."; } else { echo "The 'age' property does not exist."; } // 输出:The 'age' property does not exist.
  1. 檢查類名是否具有指定靜態屬性:
 class MyClass { public static $name = "John"; private static $age = 25; } if (property_exists('MyClass', 'name')) { echo "The 'name' static property exists."; } else { echo "The 'name' static property does not exist."; } // 输出:The 'name' static property exists.
  1. 檢查類名是否具有指定靜態私有屬性:
 class MyClass { public static $name = "John"; private static $age = 25; } if (property_exists('MyClass', 'age')) { echo "The 'age' static property exists."; } else { echo "The 'age' static property does not exist."; } // 输出:The 'age' static property does not exist.
同類函數
熱門文章